Scrapy重复用户名结果
Scrapy duplicate username result
我正在学习使用 Scrapy 做一个项目。当我尝试在线程中收集 post 的用户名(无个人信息)时,我遇到了问题。我用来收集数据的网站是https://www.eurobricks.com/forum/index.php?/forums/topic/172311-lego-star-wars-2020-set-discussion-read-first-post/。通过查看页面的HTML,发现用户名存储在这部分代码中
<a href="https://www.eurobricks.com/forum/index.php?/profile/172939-backtobricks/" data-ipshover=""
data-ipshover-target="https://www.eurobricks.com/forum/index.php?/profile/172939-backtobricks/&do=hovercard&
referrer=https%253A%252F%252Fwww.eurobricks.com%252Fforum%252Findex.php%253F%252Fforums%252Ftopic%252F172311-lego-star-wars-2020-set-discussion-read-first-post%252F"
title="Go to BacktoBricks's profile" class="ipsType_break" id="ips_uid_1558_18">BacktoBricks</a>
因此,为了获取用户名信息,我使用了以下命令
response.xpath("//a[@class='ipsType_break']/text()")
问题是结果我得到:
[<Selector xpath="//a[@class='ipsType_break']/text()" data='MKJoshA'>,
<Selector xpath="//a[@class='ipsType_break']/text()" data='MKJoshA'>,
<Selector xpath="//a[@class='ipsType_break']/text()" data='JekPorkchops'>,
<Selector xpath="//a[@class='ipsType_break']/text()" data='JekPorkchops'>,
<Selector xpath="//a[@class='ipsType_break']/text()" data='Mandalorianknight'>,
<Selector xpath="//a[@class='ipsType_break']/text()" data='Brick Cucumber'>,
<Selector xpath="//a[@class='ipsType_break']/text()" data='Brick Cucumber'>,
如您所见,即使用户只有 post 一次,用户名有时也会重复。
关于如何解决它的任何想法?是唯一有这个问题的信息,因为我也收集了关于国家的信息,我没有遇到任何问题。
会发生什么?
您尝试 select 每个具有模式 a[@class='ipsType_break']
的元素,并且每个 username
有多个元素:
尝试使用更具体的 xpath
:
//strong/a[@class='ipsType_break']/text()
或者您可以将 response.text 存储到一个集合中,这将为您提供独特的结果
names = {} #set of names
for name in names:
names.add(name) #add element to names
我正在学习使用 Scrapy 做一个项目。当我尝试在线程中收集 post 的用户名(无个人信息)时,我遇到了问题。我用来收集数据的网站是https://www.eurobricks.com/forum/index.php?/forums/topic/172311-lego-star-wars-2020-set-discussion-read-first-post/。通过查看页面的HTML,发现用户名存储在这部分代码中
<a href="https://www.eurobricks.com/forum/index.php?/profile/172939-backtobricks/" data-ipshover=""
data-ipshover-target="https://www.eurobricks.com/forum/index.php?/profile/172939-backtobricks/&do=hovercard&
referrer=https%253A%252F%252Fwww.eurobricks.com%252Fforum%252Findex.php%253F%252Fforums%252Ftopic%252F172311-lego-star-wars-2020-set-discussion-read-first-post%252F"
title="Go to BacktoBricks's profile" class="ipsType_break" id="ips_uid_1558_18">BacktoBricks</a>
因此,为了获取用户名信息,我使用了以下命令
response.xpath("//a[@class='ipsType_break']/text()")
问题是结果我得到:
[<Selector xpath="//a[@class='ipsType_break']/text()" data='MKJoshA'>,
<Selector xpath="//a[@class='ipsType_break']/text()" data='MKJoshA'>,
<Selector xpath="//a[@class='ipsType_break']/text()" data='JekPorkchops'>,
<Selector xpath="//a[@class='ipsType_break']/text()" data='JekPorkchops'>,
<Selector xpath="//a[@class='ipsType_break']/text()" data='Mandalorianknight'>,
<Selector xpath="//a[@class='ipsType_break']/text()" data='Brick Cucumber'>,
<Selector xpath="//a[@class='ipsType_break']/text()" data='Brick Cucumber'>,
如您所见,即使用户只有 post 一次,用户名有时也会重复。 关于如何解决它的任何想法?是唯一有这个问题的信息,因为我也收集了关于国家的信息,我没有遇到任何问题。
会发生什么?
您尝试 select 每个具有模式 a[@class='ipsType_break']
的元素,并且每个 username
有多个元素:
尝试使用更具体的 xpath
:
//strong/a[@class='ipsType_break']/text()
或者您可以将 response.text 存储到一个集合中,这将为您提供独特的结果
names = {} #set of names
for name in names:
names.add(name) #add element to names