Scraping: cannot access information from web

旧街凉风 提交于 2020-01-03 02:52:31

问题


I am scraping some information from this url: https://www.rockethub.com/projects/34210-lunar-lion-the-first-ever-university-led-mission-to-the-moon#description-tab

Everything was fine till I scraped the description. I tried and tried to scrape, but I failed so far. It seems like I can't reach that information. Here is my code:

html = urllib.urlopen("https://www.rockethub.com/projects/34210-lunar-lion-the-first-ever-university-led-mission-to-the-moon")
tree=BeautifulSoup(html, "lxml")
description=tree.find('div',{'id':'description_section','class':'description-section'})

Any of you has any suggestion?


回答1:


You would need to make an additional request to get the description. Here is a complete working example using requests + BeautifulSoup:

import requests
from bs4 import BeautifulSoup

url = "https://www.rockethub.com/projects/34210-lunar-lion-the-first-ever-university-led-mission-to-the-moon/"
with requests.Session() as session:
    session.headers = {
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"
    }

    # get the token
    response = session.get(url)
    soup = BeautifulSoup(response.content, "html.parser")
    token = soup.find("meta", {"name": "csrf-token"})["content"]

    # get the description
    description_url = url + "description"
    response = session.get(description_url, headers={"X-CSRF-Token": token, "X-Requested-With": "XMLHttpRequest"})

    soup = BeautifulSoup(response.content, "html.parser")
    description = soup.find('div', {'id':'description_section', 'class': 'description-section'})
    print(description.get_text(strip=True))



回答2:


I use XML package to web scraping, and I can't get the description section as you described with BeautifulSoup.

However if you just want to scrap this page only, you can download the page. Then:

page = htmlTreeParse("Lunar Lion - the first ever university-led mission to the Moon _ RocketHub.html", useInternal = TRUE,encoding="utf8")

unlist(xpathApply(page, '//div[@id="description_section"]', xmlValue))

I tried the R code to download, and I can't find the description_section either.

url="https://www.rockethub.com/projects/34210-lunar-lion-the-first-ever-university-led-mission-to-the-moon"

download.file(url,"page.html",mode="w")

Maybe we have to add some options in the function download.file. I hope that some html experts could help.




回答3:


I found out how to scrap with R:

library("rvest")

url="https://www.rockethub.com/projects/34210-lunar-lion-the-first-ever-university-led-mission-to-the-moon/description"

url %>% 
  html() %>% 
  html_nodes(xpath='//div[@id="description_section"]', xmlValue) %>%
  html_text()


来源:https://stackoverflow.com/questions/37343740/scraping-cannot-access-information-from-web

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!