从 Python 中的锚标记自动获取数据
Get data automatically from anchor tag in Python
我有一个页面内容类似于
<div class="entry">
<p>some content></p>
<a href="www.somelink.com">more</a>
</div>
在主网页中。我想在单击 link 时提取并显示数据。我在 python3.
中使用 beautifulsoup
您可以使用mainpage
参数显示提取的url内容。
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')
工作代码
divtag = soup.find_all('div', attrs={"class": "entry"})
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())
我有一个页面内容类似于
<div class="entry">
<p>some content></p>
<a href="www.somelink.com">more</a>
</div>
在主网页中。我想在单击 link 时提取并显示数据。我在 python3.
中使用 beautifulsoup您可以使用mainpage
参数显示提取的url内容。
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')
工作代码
divtag = soup.find_all('div', attrs={"class": "entry"})
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())