简体   繁体   English

Python中的re.search和urlopen

[英]re.search and urlopen in Python

I have this script : 我有这个脚本:

for url in urls:
    u = urlopen(url).read
    owner_id = re.search(r'ownerId: ([1-9]+)?,', u).group(1)
    id = re.search(r'id: ([1-9]+)?,', u).group(1)

    print(owner_id)
    print(id)

url is a list of urls url是网址列表

The script returns me "TypeError: expected string or bytes-like object" Do you have an idea how to fix that ? 该脚本向我返回“ TypeError:预期的字符串或类似字节的对象”您是否有解决办法?

Not sure what version of Python your using (below is for v3+, for v2, replace urllib with urllib2). 不确定使用的是哪个Python版本(以下是v3 +,v2,请用urllib2替换urllib)。

need to import urllib and beautiful soup 需要进口urllib和美丽的汤

import urllib
from bs4 import BeautifulSoup

url = "url address"
html = urllib.request.urlopen(url).read()
soup = BeautifulSoup(html, "lxml")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM