Crawl Amazon items list with Python

Source: Internet
Author: User

1. Careful analysis of the Amazon query detailed interface can be seen, the main key part of the three places, the three places control the query list of pages and keywords, so modify these parameters can change the number of list pages and fuzzy query results

http://www.amazon.cn/s/ref= Sr_pg_3?rh=n%3a658390051%2ck%3aphp& page=3& Keywords=java&ie=utf8&qid=1459478790 2. Changing the crawl page by replacing it with the underlying link and the regular expression matching method, note that the RE module needs to be introduced because regular expression matching is used
Source_page ='Sr_pg_'+Str (page_num) Page_index='page='+Str (page_num) newkeywords="%3a"+keywords+'&'BASEURL="http://www.amazon.cn/s/ref=sr_pg_3?rh=n%3A658390051%2Ck%3Aphp&page=3&keywords="Baseurl,number= Re.subn (r'sr_pg_[0-9]+', Source_page,baseurl) Baseurl,number= Re.subn (r'page=[0-9]+', Page_index,baseurl) Baseurl,number= Re.subn (r'%3a (. *?) /&', Newkeywords,baseurl)

3. Call F12 to view the source of the Web page, you can see the list of results that need to be crawled is within a <li></li> tag, the tag ID is incremented sequentially, to Urllib2.open (URL). Read () Gets the Web page source code with the result set after BeautifulSoup processing FindAll find can get all the book object information for a single page.
  

4. Get the name of the book and the number of sellers and offer information

  

Through the analysis of the Web page source can know to get the book name can be obtained from the #Ecoding=utf-8 fromBs4ImportBeautifulSoupImportUrllib2ImportRekeywords= Raw_input ("Please enter the book keywords you want to query:") Islast=1Page_num= 1Booklist=[] whileIslast:source_page='Sr_pg_'+Str (page_num) Page_index='page='+Str (page_num) newkeywords="%3a"+keywords+'&'BASEURL="http://www.amazon.cn/s/ref=sr_pg_3?rh=n%3A658390051%2Ck%3Aphp&page=3&keywords="Baseurl,number= Re.subn (r'sr_pg_[0-9]+', Source_page,baseurl) Baseurl,number= Re.subn (r'page=[0-9]+', Page_index,baseurl) Baseurl,number= Re.subn (r'%3a (. *?) /&', Newkeywords,baseurl) Myurl= BaseUrl +keywords#If it is accessed directly by a browser, it may be deniedUser_agent ='mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'Headers= {'user-agent': User_agent}#construct the requestreq = Urllib2. Request (myurl,headers=headers)#Visit PageMyresponse =Urllib2.urlopen (req) MyPage=Myresponse.read ()Print "--------------------------------page%d--------------------------------------"%(page_num) MyData= BeautifulSoup (MyPage,"Html.parser") Rightcontent= Mydata.findall ('Li') forIinchRightcontent:bookname= I.findall ('H2', attrs={"class":"a-size-medium a-color-null s-inline s-access-title a-text-normal"}) forBookinchBookName:Print "************************************************************************" Print("Book_name:"). Decode ('Utf-8'). Encode ('gb2312') +book.get_text () Youhui= Re.findall ('<span class= "A-color-secondary" > (. *?) </span>', str (i), re. S) forPinchYouhui:PrintP islasttrue= Mydata.findall ('span', attrs={"class":"Srsprite Lastpagerightarrow"}) ifLen (islasttrue) > 0orPage_num >= 2: Islast=0 Page_num+=1

Crawl Amazon items list with Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.