我正在读取一些正在读取html的代码,正在通过beautifulsoup进行解析,然后希望使用正则表达式来查找一些数字(作业的一部分)。TypeError使用正则表达式和beautifulsoup
现在,我使用套接字而不是urllib,我知道错误是从数据类型(期望字符串或字节),但在行我缺少我需要编码/解码来处理数据的套接字。这个错误发生在我的re.findall
除了一个修复,什么是造成这个问题,我猜更重要的是什么是数据类型的差异,因为我似乎失去了一些东西...应该感觉固有的。
提前感谢。
#Py3 urllib is utllib.request
import urllib.request
#BeautifulSoup stuff bs4 in Py3
from bs4 import *
#Raw Input now input in Py3
#url = 'http://' + input('Enter - ')
url = urllib.request.urlopen('http://python-data.dr-chuck.net/comments_42.html')
html = url.read()
#html.parser is the parser that defaults. Usefull most of the time (according to the web)
soup = BeautifulSoup(html, 'html.parser')
# Retrieve all of the tags specified
tags = soup('span')
for tag in tags:
print(re.findall('[0-9]+', tag))
准确地说,你在做什么?,因为没有定义你的代码,并且你的reggex表达式匹配所有的数字,你应该导入模块重新工作。 –
没有复制,是的,进口是有 –