python - CSS select with beautifulsoup4 doesn't work -


i tried bs4, select method doesn't work.

what's wrong code?

import requests import bs4  def main():     r = requests.get("http://nodejs.org/download/")     soup = bs4.beautifulsoup(r.text)      selector = "div.interior:nth-child(2) > table:nth-child(2) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(3) > a:nth-child(1)"     print(soup.select(selector)[0].text)  if __name__ == "__main__":     main() 

the answer on page differs viewing in browser parsing bs. have @ r.text , parse there.

the response

<div class="interior row"> <div id="installers"> <ul> <li> <a href="http://nodejs.org/dist/v0.10.26/node-v0.10.26-x86.msi"> <img alt="" height="50" src="http://nodejs.org/images/platform-icon-win.png" width="45">               windows installer               <small>node-v0.10.26-x86.msi</small> </img></a> </li> <li> <a href="http://nodejs.org/dist/v0.10.26/node-v0.10.26.pkg"> <img alt="" height="50" src="http://nodejs.org/images/platform-icon-osx.png" width="45">               macintosh installer               <small>node-v0.10.26.pkg</small> 

so there no table here. hope helps.

edit: code following response:

def main():     r = requests.get("http://nodejs.org/download/")     soup = bs4.beautifulsoup(r.text)     # print r.text     selector = "div.interior"     print(soup.select(selector)[2]) 

edit 2: try find. mor flexible one.

soup = bs4.beautifulsoup(r.text) print(soup.find("a", text="64-bit")) 

edit 3: should work:

def main():     r = requests.get("http://nodejs.org/download/", headers={"content-type":"text", "user-   agent": "mozilla/5.0 (windows nt 6.1; wow64) applewebkit/536.5 (khtml, gecko) chrome/19.0.1084.52 safari/536.5"})     soup = bs4.beautifulsoup(r.text)     print(soup.find("table").tr.td.findnextsibling().a['href']) 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -