如何获取html页面中的所有标记和信息(特别是页面中的所有链接)?

2024-09-27 19:20:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用BeautifulSoup获取此页上所有可用的链接。 但是,在使用urllib获取URL,然后使用BeautifulSoup对其进行解析时,我并没有返回此页面上的所有可用信息。你知道吗

我试过不同的解析器(html.parser语法分析器,lxml,xml,html5lib),但它不会返回所需的结果。你知道吗

我知道如何获取标记的详细信息,但是我存储html数据的文件中不包含可用的链接。但当我检查chrome上的元素时,它确实显示了链接。下面是我正在处理的URL的代码:

def fetch_html(fullurl,contextstring):
    print("Opening the file connection for " + fullurl)
    uh= urllib.request.urlopen(fullurl, context=contextstring)
    print("HTTP status",uh.getcode())
    html =uh.read() 
    bs = BeautifulSoup(html, 'lxml')
    return bs
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
mainurl ='https://www.daad.de/deutschland/studienangebote/international-programmes/en/result/?q=&degree%5B%5D=2&lang%5B%5D=2&fos=3&crossFac=&cert=&admReq=&scholarshipLC=&scholarshipSC=&langDeAvailable=&langEnAvailable=&lvlEn%5B%5D=&cit%5B%5D=&tyi%5B%5D=&fee=&bgn%5B%5D=&dur%5B%5D=&sort=4&ins%5B%5D=&subjects%5B%5D=&limit=10&offset=&display=list'
a=(fetch_html(mainurl, ctx))
f= open("F:\Harsh docs\python\courselinks.py","w")
f.write(a.prettify())
f.close

因此,我有兴趣获得一个“嵌入式系统(ESY)”的链接。你知道吗


Tags: urlsslbs链接htmlcontextfetchurllib
2条回答

你正在抓取的页面似乎是用javascript呈现的。 你可以尝试使用硒和铬。 或者您可以使用requests\uhtml包https://html.python-requests.org/ 在获取html之前呈现javascript

只需从页面获取所有链接,请使用下面的代码:(python3)

from bs4 import BeautifulSoup
import re
from urllib.request import urlopen

html_page = urlopen("http://www.google.com/")
soup = BeautifulSoup(html_page)
for link in soup.findAll('a', attrs={'href': re.compile("^http://")}):
    print (link.get('href'))

相关问题 更多 >

    热门问题