我是Python的新手,並試圖在Python中創建一個腳本,該腳本會刮擦一個網站並在幾個鏈接中返回文本。出於某種原因,我不明白爲什麼這不起作用,並想知道爲什麼。我的正則表達式是:正則表達式在Python中沒有得到我想要的結果
> regex = re.compile(r'<a target="_blank" title=".+" href=".+.pdf">(.+)</a>')
全碼:
import requests, re
response = requests.get('websithere')
websiteDate = response.text
regex = re.compile(r'<a target="_blank" title=".+" href=".+.pdf">(.+)</a>')
mo = regex.findall(websiteDate)
print(mo)
我把(+)組中認爲它會發現在那裏列出的任何文字。該3個鏈接它通過掃描爲:
> <a target="_blank" title="Farm Business & Production Management
> Instructor" href="/uploadedpdfs/job-opportunities/Farm Business
> Production Mgt Instructor 8-17.pdf">Farm Business & Production
> Management Instructor</a>
>
> <a target="_blank" title="Paramedic Tech Adjunct Instructor Aide"
> href="/uploadedpdfs/job-opportunities/Paramedic Adjunct Instructor
> Aide.pdf">Paramedic Tech Adjunct Instructor Aide</a>
>
> <a target="_blank" title="Technology Support Specialist"
> href="/uploadedpdfs/job-opportunities/Technology Support
> Specialist.pdf">Technology Support Specialist</a>
而不是我的結果只返回: 「技術支持專家」
什麼我錯在這裏做什麼?我只是試圖返回標籤內的文字。我嘗試了一下,並且無法使其工作。任何幫助,將不勝感激。
謝謝!
您爲執行文章中顯示的輸出而執行哪條語句?請粘貼所有相關的代碼。作爲一個附註,不要使用REGEX來分析HTML。 https://stackoverflow.com/questions/6751105/why-its-not-possible-to-use-regex-to-parse-html-xml-a-formal-explanation-in-la。使用BeautifulSoup。 – DyZ
不要使用正則表達式來解析html。 –