简体   繁体   中英

Scraping web contents from first two page and export scraped data to csv using python and BS4

I m new to python and using Python 3.6.2 and I m trying to scrape data from first 2 page using a specific keyword. So far I m able to get the data into Python IDLE window, but I m facing difficulty in exporting data to CSV.I have tried using BeautifulSoup 4 and pandas but not able to export. Here is the so far what I have done. Any help would be much appreciated.

import csv   
import requests
from bs4 import BeautifulSoup
import pandas as pd

url = "http://www.amazon.in/s/ref=nb_sb_noss?url=search-
alias%3Dautomotive&field-
keywords=helmets+for+men&rh=n%3A4772060031%2Ck%3Ahelmets+for+men&ajr=0"
request = requests.get(url)    
soup = BeautifulSoup(request.content, "lxml")
#filename = auto.csv
#with open(str(auto.csv,"r+","\n")) as csvfile:
    #headers = "Count , Asin \n"
    #fo.writer(headers)
for url in soup.find_all('li'):
    Nand = url.get('data-asin')
    #print(Nand)   
    Result = url.get('id')
    #print(Result)
    #d=(str(Nand), str(Result))


df=pd.Index(url.get_attribute('url'))
#with open("auto.txt", "w",newline='') as dumpfile:
   #dumpfilewriter = csv.writer(dumpfile)
   #for Nand in soup:
       #value =  Nand.__gt__        
       #if value:
           #dumpfilewriter.writerows([value])
df.to_csv(dumpfile)
dumpfile.close()
csvfile.csv.writer("auto.csv," , ',' ,'|' , "\n")

Question : Help me with exporting the data of variable "Nand" and "Result" to csv file

with open("auto.csv", 'w') as fh:
    writer = csv.DictWriter(fh, fieldnames=['Nand', 'Result'])
    writer.writeheader()
    data = {}
    for url in soup.find_all('li'):
        data['Nand'] = url.get('data-asin')
        data['Result'] = url.get('id')
        writer.writerow(data)

Tested with Python: 3.4.2

I added user-agent in request to site to escape auto blocking bots. You got a lot of None because you didn't specify which precisely <li> tags do you want. I added it to code as well.

import requests
from bs4 import BeautifulSoup
import pandas as pd


url = "http://www.amazon.in/s/ref=nb_sb_noss?url=search-alias%3Dautomotive&field-keywords=helmets+for+men&rh=n%3A4772060031%2Ck%3Ahelmets+for+men&ajr=0"
request = requests.get(url, headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36'})    
soup = BeautifulSoup(request.content, "lxml")

res = []

for url in soup.find_all('li', class_ = 's-result-item'):
    res.append([url.get('data-asin'), url.get('id')])

df = pd.DataFrame(data=res, columns=['Nand', 'Result'])    
df.to_csv('path/where/you/want/to/store/file.csv')

EDIT : for processing all pages you need to build a loop that generates urls which you will then pass to main processing block (which you already have). Check out this page: http://www.amazon.in/s/ref=sr_pg_2?rh=n%3A4772060031%2Ck%3Ahelmets+for+men&page=2&keywords=helmets+for+men&ie=UTF8&qid=1501133688&spIA=B01N0MAT2E,B01MY1ZZDS,B01N0RMJ1H .

EDIT_2 : let's loop on page parameter. You can manually add page to url which you pass to requests.get() .

import requests
from bs4 import BeautifulSoup
import pandas as pd

base_url = "http://www.amazon.in/s/ref=sr_pg_2?rh=n%3A4772060031%2Ck%3Ahelmets+for+men&keywords=helmets+for+men&ie=UTF8"
#excluding page from base_url for further adding
res = []

for page in range(1,72): # such range is because last page for needed category is 71

    request = requests.get(base_url + '&page=' + str(page), headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36'}) # here adding page    
    if request.status_code == 404: #added just in case of error
        break
    soup = BeautifulSoup(request.content, "lxml")

    for url in soup.find_all('li', class_ = 's-result-item'):
        res.append([url.get('data-asin'), url.get('id')])

df = pd.DataFrame(data=res, columns=['Nand', 'Result'])    
df.to_csv('path/where/you/want/to/store/file.csv')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM