简体   繁体   中英

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

The script returns me "TypeError: expected string or bytes-like object" Do you have an idea how to fix that ?

Not sure what version of Python your using (below is for v3+, for v2, replace urllib with urllib2).

need to import urllib and beautiful soup

import urllib
from bs4 import BeautifulSoup

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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