我一直在努力嘗試這個。刮一個論壇帖子:如何從css margin屬性計算後續關係?
我的刮臉工作的目標網站是一個老式論壇,在他們的索引頁面中,每個線程都在<div>
標籤中,每個帖子都在<p>
標籤中。後續帖子的左邊距縮進20px表示關係。
<div>
<p style="margin:2px 0 17px 0px; width:705px"><a href="./6368972.html" class="post">original post</a>other stuff</p>
<p style="margin:2px 0 2px 20px; width:683px"><a href="./6368973.html" class="post">reply post</a>other stuff</p>
<p style="margin:2px 0 2px 40px; width:661px"><a href="./6368974.html" class="post">reply post</a>other stuff</p>
...
</div>
我能提取很多信息來源這裏包括標題,日期時間,暱稱等,但後續的關係,我需要你的一個有效的算法幫助。基本上我需要知道這篇文章是對之前文章的回覆。
我的項目包含後續關係的字段,即: reply_to = scrapy.Field()
其中字段應存儲reply_to帖子的url。
我可以爲每個崗位作爲提取左邊距的值: margin = int(div.css('p::attr(style)').re('.* (\d+)px;.*'))
另外我可以計算div的長度(即,許多總訊息如何在那裏在一個線程)。
但真的不知道我怎麼會從這裏走......
謝謝大家!
1--------------------- # left margin = 0px; original post
2 ------------------- # left margin = 20px; reply to post 1
3 ----------------- # left margin = 40px; reply to post 2
4 ------------------- # left margin = 20px; reply to post 1, not 3
5 ----------------- # left margin = 40px; reply to post 4, not 2
6 --------------- # left margin = 60px; reply to post 5
目前尚不清楚你到底想要做什麼。你可以分享有關提供的HTML源文件的所需輸出嗎? – Andersson