在爬虫中取元素的值有多种方法,下面是几种常用的方法:
import re
html = "<a href='https://www.example.com'>Example</a>"
links = re.findall(r"<a.*?href=['\"](.*?)['\"].*?>(.*?)</a>", html)
for link in links:
url = link[0]
text = link[1]
print("URL:", url)
print("Text:", text)
from bs4 import BeautifulSoup
html = "<h1>This is a title</h1>"
soup = BeautifulSoup(html, 'html.parser')
titles = soup.find_all('h1')
for title in titles:
print("Title:", title.text)
from lxml import etree
html = "<p>This is a paragraph.</p>"
tree = etree.HTML(html)
paragraphs = tree.xpath('//p')
for paragraph in paragraphs:
print("Text:", paragraph.text)
这些都是常见的方法,具体使用哪种方法取决于你所爬取的网站和数据结构的特点。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: python运行函数时如何调试