我有一个包含
等内容的页面<div class="entry">
<p>some content></p>
<a href="www.somelink.com">more</a>
</div>
在主网页中。我想在点击链接时提取并显示数据。我在python3中使用beautifulsoup。
答案 0 :(得分:0)
您可以使用mainpage
参数显示提取的网址的内容。
code = '''<div class="entry">
<p>some content></p>
<a href="www.somelink.com">more</a>
</div>'''
soup = BeautifulSoup(code, 'html.parser')
divtag = soup.find('div', attrs={"class": "entry"})
a_tags = divtag.find_all('a')
for a in a_tags:
url = a.get('href')
response = requests.get(url)
mainpage = BeautifulSoup(response.text, 'html5lib')
答案 1 :(得分:0)
工作代码
divtag = soup.find_all(&#39; div&#39;,attrs = {&#34; class&#34;:&#34; entry&#34;})
for a in divtag:
a_tag = a.find('a')
url = a_tag.get('href')
response = requests.get(url)
mainpage = BeautifulSoup(response.text, 'html5lib')
divtag1 = mainpage.find('div', attrs={"class": "entry"})
a_tag1 = divtag1.find('p')
print(a_tag1.get_text())