我一直在开发一个python网络爬虫,从这个网站收集二手车库存数据(http://www.bobaedream.co.kr/cyber/CyberCar.php?gubun=I&page=20)
首先,我只想从列表中收集“宝马”。所以,我在正则表达式中使用了“search”函数,如下代码所示。但是,它总是返回“无”
我的代码有什么错误吗
请给我一些建议
谢谢
from bs4 import BeautifulSoup
import urllib.request
import re
CAR_PAGE_TEMPLATE = "http://www.bobaedream.co.kr/cyber/CyberCar.php?gubun=I&page="
def fetch_post_list():
for i in range(20,21):
URL = CAR_PAGE_TEMPLATE + str(i)
res = urllib.request.urlopen(URL)
html = res.read()
soup = BeautifulSoup(html, 'html.parser')
table = soup.find('table', class_='cyber')
print ("Page#", i)
# 50 lists per each page
lists=table.find_all('tr', itemtype="http://schema.org/Article")
count=0
r=re.compile("[BMW]")
for lst in lists:
if lst.find_all('td')[3].find('em').text:
lst_price=lst.find_all('td')[3].find('em').text
lst_title=lst.find_all('td')[1].find('a').text
lst_link = lst.find_all('td')[1].find('a')['href']
lst_photo_url=''
if lst.find_all('td')[0].find('img'):
lst_photo_url = lst.find_all('td')[0].find('img')['src']
count+=1
else: continue
print('#',count, lst_title, r.search("lst_title"))
return lst_link
fetch_post_list()
这是在字符串文本
"lst_title"
内搜索,而不是名为lst_title
的变量,这就是它从不匹配的原因方括号表示您正在查找其中一个字符。因此,例如,任何包含
M
的字符串都将匹配。你只需要"BMW"
。事实上,您甚至不需要正则表达式,只需测试:相关问题 更多 >
编程相关推荐