【问题标题】:Could not able to extract #document from HTML file through python web scraping无法通过python网络抓取从HTML文件中提取#document
【发布时间】:2017-08-14 14:41:08
【问题描述】:

当我检查浏览器上的元素时,我可以清楚地看到确切的网页内容。但是当我尝试运行以下脚本时,我看不到某些网页的详细信息。在网页中,我看到“#document”元素在我运行脚本时丢失了。如何查看#document 元素的详细信息或使用脚本进行提取?

from bs4 import BeautifulSoup
import requests

response = requests.get('http://123.123.123.123/')
soup = BeautifulSoup(response.content, 'html.parser')
print soup.prettify()

【问题讨论】:

标签: python html web-scraping beautifulsoup


【解决方案1】:

您还需要提出其他请求以获取frame 页面内容:

from urlparse import urljoin

from bs4 import BeautifulSoup
import requests

BASE_URL = 'http://123.123.123.123/'

with requests.Session() as session:
    response = session.get(BASE_URL)
    soup = BeautifulSoup(response.content, 'html.parser')

    for frame in soup.select("frameset frame"):
        frame_url = urljoin(BASE_URL, frame["src"])

        response = session.get(frame_url)
        frame_soup = BeautifulSoup(response.content, 'html.parser') 
        print(frame_soup.prettify())

【讨论】:

    猜你喜欢
    • 2020-03-17
    • 1970-01-01
    • 1970-01-01
    • 2019-08-20
    • 2022-11-17
    • 1970-01-01
    • 2021-10-27
    • 2014-10-28
    • 1970-01-01
    相关资源
    最近更新 更多