gpt4 book ai didi

python - 匹配网页内容中的文本但抛出索引错误

转载 作者:太空宇宙 更新时间:2023-11-04 03:35:00 24 4
gpt4 key购买 nike

此代码抛出错误索引超出范围错误

import os
import re

url = "http://www.jabong.com/purys-Beige-Shirts-1059637.html"
wget_data = os.popen('wget -qO- %s'% url).read()
data = re.findall(r'c999 fs12 mt10 f-bold">(.*)<\/table',wget_data)[0]
print data

输出:

Traceback (most recent call last):
File "variable_concat.py", line 7, in <module>
images = re.findall(r'c999 fs12 mt10 f-bold">(.*)<\/table',wget_data)[0]
IndexError: list index out of range

这是网页内容中的一个大字符串,我如何匹配它?

r'c999 fs12 mt10 f-bold">(.*)<\/table'

最佳答案

使用 BeautifulSoup 解析器。

import os
import re
from bs4 import BeautifulSoup
url = "http://www.jabong.com/purys-Beige-Shirts-1059637.html"
wget_data = os.popen('wget -qO- %s'% url).read()
soup = BeautifulSoup(wget_data)
print soup.find('table', class_="c999 fs12 mt10 f-bold").contents

如果你真的想使用正则表达式,那么你需要启用 DOTALL 修饰符。因为 . 默认情况下不会匹配换行符(\n\r)。

import os
import re

url = "http://www.jabong.com/purys-Beige-Shirts-1059637.html"
wget_data = os.popen('wget -qO- %s'% url).read()
data = re.findall(r'(?s)c999 fs12 mt10 f-bold">(.*?)<\/table',wget_data)[0]
print data

关于python - 匹配网页内容中的文本但抛出索引错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29506046/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com