![](/img/trans.png)
[英]how to use regex if I am looking for a specific word in entire string
[英]How do I parse through string looking for specific word/digits and display them if found
我确定我已经写了一些相当可疑的代码,但这似乎可以解决问题。 问题是,如果广告中的第一个单词不是年份,它会将数据打印到电子表格中,并希望在该列中查找车辆的年份,然后显示可能是制造商的第一个单词。
本质上,我想设置if语句,以便如果车辆年份不在第一个单词中,而是在字符串中的其他位置,它仍会找到它并将其打印到我的.csv中。
另外,我一直在努力解析多个页面,并希望这里的人也可以提供帮助。 网址中包含page = 2等,但我无法通过所有网址解析它并获取所有页面上的数据。 目前,我尝试过的所有内容都只显示首页。 您可能已经猜到了,我是Python的新手。
import csv ; import requests
from bs4 import BeautifulSoup
outfile = open('carandclassic-new.csv','w', newline='', encoding='utf-8')
writer = csv.writer(outfile)
writer.writerow(["Link", "Title", "Year", "Make", "Model", "Variant", "Image"])
url = 'https://www.carandclassic.co.uk/cat/3/?page=2'
get_url = requests.get(url)
get_text = get_url.text
soup = BeautifulSoup(get_text, 'html.parser')
car_link = soup.find_all('div', 'titleAndText', 'image')
for div in car_link:
links = div.findAll('a')
for a in links:
link = ("https://www.carandclassic.co.uk" + a['href'])
title = (a.text.strip())
year = (title.split(' ', 1)[0])
make = (title.split(' ', 2)[1])
model = (title.split(' ', 3)[2])
date = "\d"
for line in title:
yom = title.split()
if yom[0] == "\d":
yom[0] = (title.split(' ', 1)[0])
else:
yom = title.date
writer.writerow([link, title, year, make, model])
print(link, title, year, make, model)
outfile.close()
请有人可以帮我吗? 我意识到底部的if语句可能会偏离。
该代码成功地从字符串中获取了第一个单词,可惜的是,数据的结构方式并不总是汽车的制造年份(yom)
注释
"1978 Full restored Datsun 280Z"
变为'1978' '1978' '280Z'
。
而不是'1978' 'Datsun' '280z'
为了改善year
验证,请更改为使用re
模块:
import re
if not (len(year) == 4 and year.isdigit()):
match = re.findall('\d{4}', title)
if match:
for item in match:
if int(item) in range(1900,2010):
# Assume year
year = item
break
输出变为:
'1978 Full restored Datsun 280Z', '1978', 'Full', '280Z'
关于错误结果make='Full'
您有两种选择。
停用词清单
用['full', 'restored', etc.]
术语构建停用词列表,并loop
title_items
以查找不在停用词列表中的第一项。
厂商清单
建立一个像['Mercedes', 'Datsun', etc.]
这样的制造商列表['Mercedes', 'Datsun', etc.]
并loop
title_items
来找到第一个匹配的项目。
问题 :如果广告中的第一个单词不是年份,请查找车辆的年份
二手的build-in
和module
:
使用的样本标题:
# Simulating html Element class Element(): def __init__(self, text): self.text = text for a in [Element('Mercedes Benz 280SL 1980 Cabriolet in beautiful condition'), Element('1964 Mercedes Benz 220SEb Saloon Manual RHD')]:
从<a
Element
获取title
,并用blanks
分隔。
title = a.text.strip() title_items = title.split()
默认title_items
index 0, 1, 2
处的title_items
。
# Default year = title_items[0] make = title_items[1] model = title_items[2]
验证year
满足条件4位数字
# Verify 'year' if not (len(year) == 4 and year.isdigit()):
循环title_items
所有item
,如果满足条件则中断。
# Test all items for item in title_items: if len(item) == 4 and item.isdigit(): # Assume year year = item break
更改为假设,索引0, 1
处的title_items
是make
和model
make = title_items[0] model = title_items[1]
检查model
是否以数字开头
注意 :如果模型不符合此条件,将失败!
# Condition: Model have to start with digit if not model[0].isdigit(): for item in title_items: if item[0].isdigit() and not item == year: model = item print('{}'.format([title, year, make, model]))
输出 :
['Mercedes Benz 280SL 1980 Cabriolet in beautiful condition', '1980', 'Mercedes', '280SL'] ['1964 Mercedes Benz 220SEb Saloon Manual RHD', '1964', 'Mercedes', '220SEb']
使用Python测试:3.4.2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.