Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Learning WebScraping
#11
Like this:
from bs4 import BeautifulSoup
from urllib.request import urlopen

page_url = "http://econpy.pythonanywhere.com/ex/001.html"
new_file = "Mynew.csv"
f = open(new_file, "w")
Headers = "Header1,Header2\n"
f.write(Headers)

html = urlopen(page_url)
soup = BeautifulSoup(html, "html.parser")
buyer_info = soup.find_all("div", {"title":"buyer-info"})
for i in buyer_info:
    Header1 = i.find("div", {"title":"buyer-name"})
    Header2 = i.find("span", {"class":"item-price"})
    f.write('{},{}\n'.format(Header1.text, Header2.text))
f.close()
Output:
Header1,Header2 Carson Busses,$29.95 Earl E. Byrd,$8.37 Patty Cakes,$15.26 Derri Anne Connecticut,$19.25 Moe Dess,$19.25 .......
Reply
#12
oh yes..oh Yes...Like this only....like this only...oh yes. like this only...., now last thing...how to scrape in this code for next pages also....i mean how to create loop for multiple pages also?
Reply
#13
(Aug-29-2017, 09:48 AM)Prince_Bhatia Wrote: i mean how to create loop for multiple pages also?
I did show you before in post.
It would look like this put together.
from bs4 import BeautifulSoup
from urllib.request import urlopen

new_file = "Mynew.csv"
f = open(new_file, "w")
Headers = "Header1,Header2\n"
f.write(Headers)
for page in range(1, 5):
    page_url = "http://econpy.pythonanywhere.com/ex/00{}.html".format(page)
    html = urlopen(page_url)
    soup = BeautifulSoup(html, "html.parser")
    buyer_info = soup.find_all("div", {"title": "buyer-info"})
    for i in buyer_info:
        Header1 = i.find("div", {"title": "buyer-name"})
        Header2 = i.find("span", {"class": "item-price"})
        f.write('{},{}\n'.format(Header1.text, Header2.text))
f.close()
Reply
#14
Thank you so much. this is wonderful experience here. Thank you all for your help.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Intro to WebScraping d1rjr03 3 5,527 Dec-16-2024, 02:50 AM
Last Post: bobprogrammer
  Webscraping - loop on first page RikP 0 743 Jul-22-2024, 12:15 PM
Last Post: RikP
  Webscraping news articles by using selenium cate16 7 6,070 Aug-28-2023, 09:58 AM
Last Post: snippsat
  Webscraping with beautifulsoup cormanstan 3 8,021 Aug-24-2023, 11:57 AM
Last Post: snippsat
  Webscraping returning empty table Buuuwq 0 2,515 Dec-09-2022, 10:41 AM
Last Post: Buuuwq
  WebScraping using Selenium library Korgik 0 1,633 Dec-09-2022, 09:51 AM
Last Post: Korgik
  How to get rid of numerical tokens in output (webscraping issue)? jps2020 0 2,506 Oct-26-2020, 05:37 PM
Last Post: jps2020
  Python Webscraping with a Login Website warriordazza 0 3,364 Jun-07-2020, 07:04 AM
Last Post: warriordazza
  Help with basic webscraping Captain_Snuggle 2 5,322 Nov-07-2019, 08:07 PM
Last Post: kozaizsvemira
  Can't Resolve Webscraping AttributeError Hass 1 3,015 Jan-15-2019, 09:36 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020