![](/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.