在爬虫中取元素的值有多种方法,下面是几种常用的方法:
import re
html = "Example"
links = re.findall(r"(.*?)", html)
for link in links:
url = link[0]
text = link[1]
print("URL:", url)
print("Text:", text)
登录后复制
from bs4 import BeautifulSoup
html = "This is a title"
soup = BeautifulSoup(html, 'html.parser')
titles = soup.find_all('h1')
for title in titles:
print("Title:", title.text)
登录后复制
from lxml import etree
html = "
This is a paragraph.
"
tree = etree.HTML(html)
paragraphs = tree.xpath('//p')
for paragraph in paragraphs:
print("Text:", paragraph.text)
登录后复制
这些都是常见的方法,具体使用哪种方法取决于你所爬取的网站和数据结构的特点。
以上就是python怎么在爬虫中取元素里的值的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!