如果以前的元素是x,请返回
我已经弄清楚了如何通过使用 present_sibling
“vyvěšeno”
和“ sejmuto”
/code>,但是现在我需要将所有divs(分为一个变量)。我认为有些陈述会有所帮助。
有时有1个,有时在上方最多3个div。
来自我当前数组的示例:
['11.\xa0veřejné zasedání zastupitelstva obce se uskuteční 21.\xa012.\xa02011 v\xa019.30\xa0v\xa0budově obecního\xa0úřadu.', 'Vyvěšeno: 13. 12. 2011', 'Sejmuto: 21. 12. 2011']
代码:
from bs4 import BeautifulSoup
import requests
import re
from csv import writer
url= "https://www.valchov.cz/sluzby/specialni-sluzby-/"
page = requests.get(url)
soup = BeautifulSoup(page.content, "html.parser")
sejmuto = soup.find_all("p", string=re.compile("Sejmuto:"))
with open("listings.csv", "w", encoding="utf8") as f:
thewriter = writer(f)
header= ["Name", "Name bezdiakritikyamezer" , "URL", "Zveřejněno", "Sejmuto"]
thewriter.writerow(header)
for hhh in sejmuto:
item1 = hhh.previous_sibling.previous_sibling.text
itemz = hhh.previous_sibling.previous_sibling.previous_sibling.previous_sibling.text
item2 = (hhh.text)
item = [itemz, item1, item2]
print(item)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题和预期的输出并不清楚,但假设您的目标是获取所有链接并应用相应的日期,我将指定以这种方式调整您的脚本:
您不需要额外的
re re
模型改为使用CSS选择器
:选择并迭代所有
find_previous_siblings()
,检查它是否包含<<代码>&lt; a&gt; 并将您的行写入CSV else break for for for for loop并继续:注意/strong> 有一些不规则且不正确的拼写 /标点符号(“:”,“。”或其中没有)< /
url
>
...
Question and expected output is not that clear but assuming, that your goal is to get all the links and apply corresponding dates I would reccomend to adjust your script that way:
You do not need extra
re
modul instead usecss selectors
:Select and iterate all
find_previous_siblings()
, check if it contains a<a>
and write your row to csv else break the for loop and continue:Note There are some irregular and incorrect spellings / punctuation (":","." or no of these)
Example
Output
...