关于python的正则表达式有一些疑问
好吧,我已经有这个脚本了:
#!/usr/bin/python3
import requests
from bs4 import BeautifulSoup
import re
def get_html(url):
r = requests.get(url)
return r.content
url = "https://foxnews.com/"
html = get_html(url)
pattern = re.compile(r'(https?\/\/).*\.(jpg|jpeg|png)')
matches = re.findall(pattern, html)
for match in matches:
print(match)
但是,我会收到一个错误:typeError:无法在类似字节的对象上使用字符串模式,
我该如何获得此功能,以便我使用Regex从网站上刮下来的HTML找到映像链接?
Ok, I've got this script:
#!/usr/bin/python3
import requests
from bs4 import BeautifulSoup
import re
def get_html(url):
r = requests.get(url)
return r.content
url = "https://foxnews.com/"
html = get_html(url)
pattern = re.compile(r'(https?\/\/).*\.(jpg|jpeg|png)')
matches = re.findall(pattern, html)
for match in matches:
print(match)
But, I get an error: TypeError: cannot use a string pattern on a bytes-like object
How can I get this so that I use regex to find image links from the HTML I scraped from websites?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
r.content
返回字节中的内容,而您需要将Unicode文本在其上使用。r.content
returns the content in bytes, instead you need to get the unicode text to use regex on it.