如何在CSS选择器中加入OR/AND逻辑实现特定网页爬取需求?
Absolutely, you can use CSS's built-in comma separator to implement OR logic here—this lets you target both H2 and H3 elements matching your condition in a single selector, which will also fix your CSV export error.
The root issue with your original code is that you’re defining the same 'Researcher' key twice in your dictionary. In Python dictionaries, duplicate keys get overwritten, so only one of your selector results was being kept (hence the broken CSV output). Merging the selectors into one solves this by gathering all matching <p> texts into a single list under a single key.
Here’s the corrected code line:
'Researcher': response.css(".field-item.even h2:contains(RESEARCHER) + p ::text, .field-item.even h3:contains(RESEARCHER) + p ::text").extract()
Quick breakdown of the selector:
- The comma
,acts as the OR operator, splitting the selector into two valid CSS queries:.field-item.even h2:contains(RESEARCHER) + p ::textgrabs text from<p>elements directly following H2 tags with "RESEARCHER".field-item.even h3:contains(RESEARCHER) + p ::textdoes the same for H3 tags
- This combined selector returns all matching text content in one list, which you can export to CSV without duplicate key conflicts.
For context, if you ever need AND logic in CSS selectors (not required here), you’d chain conditions without separators—like h2.active:contains(RESEARCHER) to target H2 tags that have both the active class and contain "RESEARCHER".
内容的提问来源于stack exchange,提问作者Mr_Snuffles

