Python 将LXML与Html、请求和ETree一起使用,它提供链接,但不允许我搜索特定文本的链接

Python 将LXML与Html、请求和ETree一起使用,它提供链接,但不允许我搜索特定文本的链接,python,web-scraping,lxml,Python,Web Scraping,Lxml,我试图从下面提供的链接中提取特定数据。当我运行代码时,它会按预期提供所有href链接,但当我尝试对同一字符串进行进一步测试,但使用contains语法时,它返回为空 我查看了文档和DevHints,无论我在哪里查找,Contains语法都是捕获我要查找的内容的推荐方法,而我只知道语法将包括在内,但不包括在何处或如何包含 我正试图建立一个刮刀,以帮助许多人最近下岗找到新的工作,所以任何援助是非常感谢 代码: 基于组的解决方案 from bs4 import BeautifulSoup import

我试图从下面提供的链接中提取特定数据。当我运行代码时,它会按预期提供所有href链接,但当我尝试对同一字符串进行进一步测试,但使用contains语法时,它返回为空

我查看了文档和DevHints,无论我在哪里查找,Contains语法都是捕获我要查找的内容的推荐方法,而我只知道语法将包括在内,但不包括在何处或如何包含

我正试图建立一个刮刀,以帮助许多人最近下岗找到新的工作,所以任何援助是非常感谢

代码:


基于组的解决方案

from bs4 import BeautifulSoup
import requests

page = requests.get('https://ea.gr8people.com/index.gp?method=cappportal.showPortalSearch&sysLayoutID=123').content

soup = BeautifulSoup(page, 'html.parser')
links = soup.find_all('a')
links = [a for a in links if a.attrs.get('href') and 'opportunityid' in a.attrs.get('href')]
print('-- opportunities --')
for idx, link in enumerate(links):
    print('{}) {}'.format(idx, link))
输出

-- opportunities --
0) <a href="index.gp?method=cappportal.showJob&amp;layoutid=2092&amp;inp1541=&amp;inp1375=154761&amp;opportunityid=154761">
                                        2D Capture Artist - 6 month contract
                                    </a>
1) <a href="index.gp?method=cappportal.showJob&amp;layoutid=2092&amp;inp1541=&amp;inp1375=154426&amp;opportunityid=154426">
                                        Accounting Supervisor
                                    </a>
2) <a href="index.gp?method=cappportal.showJob&amp;layoutid=2092&amp;inp1541=&amp;inp1375=152147&amp;opportunityid=152147">
                                        Advanced Analyst
                                    </a>
3) <a href="index.gp?method=cappportal.showJob&amp;layoutid=2092&amp;inp1541=&amp;inp1375=153395&amp;opportunityid=153395">
                                        Advanced UX Researcher
                                    </a>
4) <a href="index.gp?method=cappportal.showJob&amp;layoutid=2092&amp;inp1541=&amp;inp1375=151309&amp;opportunityid=151309">
                                        AI Engineer
                                    </a>
5) <a href="index.gp?method=cappportal.showJob&amp;layoutid=2092&amp;inp1541=&amp;inp1375=150468&amp;opportunityid=150468">
                                        AI Scientist
                                    </a>
6) <a href="index.gp?method=cappportal.showJob&amp;layoutid=2092&amp;inp1541=&amp;inp1375=151310&amp;opportunityid=151310">
                                        AI Scientist - NLP Focus
                                    </a>
7) <a href="index.gp?method=cappportal.showJob&amp;layoutid=2092&amp;inp1541=&amp;inp1375=153351&amp;opportunityid=153351">
                                        AI Software Engineer (Apex Legends)
                                    </a>
8) <a href="index.gp?method=cappportal.showJob&amp;layoutid=2092&amp;inp1541=&amp;inp1375=152737&amp;opportunityid=152737">
                                        AI Software Engineer (Frostbite)
                                    </a>
9) <a href="index.gp?method=cappportal.showJob&amp;layoutid=2092&amp;inp1541=&amp;inp1375=154764&amp;opportunityid=154764">
                                        Analyste Qualité Sénior / Senior Quality Analyst
                                    </a>
10) <a href="index.gp?method=cappportal.showJob&amp;layoutid=2092&amp;inp1541=&amp;inp1375=153948&amp;opportunityid=153948">
                                        Animator 1
                                    </a>
11) <a href="index.gp?method=cappportal.showJob&amp;layoutid=2092&amp;inp1541=&amp;inp1375=151353&amp;opportunityid=151353">
                                        Applications Agreement Analyst
                                    </a>
12) <a href="index.gp?method=cappportal.showJob&amp;layoutid=2092&amp;inp1541=&amp;inp1375=154668&amp;opportunityid=154668">
                                        AR Analyst I
                                    </a>
13) <a href="index.gp?method=cappportal.showJob&amp;layoutid=2092&amp;inp1541=&amp;inp1375=153609&amp;opportunityid=153609">
                                        AR Specialist
                                    </a>
14) <a href="index.gp?method=cappportal.showJob&amp;layoutid=2092&amp;inp1541=&amp;inp1375=154773&amp;opportunityid=154773">
                                        Artiste Audio / Audio Artist
                                    </a>

我在这里也看过这个页面:它显示了几乎相同的语法,特别是当与contains语句相关时,所以我不确定为什么它不起作用,除非它必须明确匹配其完整性中的文本,我的文本是…&opportunityid=XXXXXX的一部分,但是我不知道它是否必须有完整的文本。我不认为它有,也没有看到任何地方有这样的说法。如果opportunityid在href属性中,您应该能够将xpath更改为//*/a[contains@href,opportunityid]。您也不应该需要*..//a[contains@href我刚刚检查了这个,这个也起作用了。非常感谢您的帮助!它看起来格外干净,以及在输出方面!这绝对是完美的!非常感谢。是否有任何基于此解决方案的文档可供您推荐,以帮助我进一步了解它?我知道其中的大部分,我可以解释其余的大部分,但其中一些我以前没有见过,比如links=part,我不知道我可以用带有漂亮汤的if语句,更具体地说是and语句来做for。我之前试过好几次,他们只是简单地拒绝了我的语法,所以任何关于这方面的注释都会非常棒!我很乐意帮忙:-。代码中没有“魔力”——只有BS API和python列表理解。您有没有建议或最佳实践方法让我修改URL,使其与页面的基本URL保持一致,以便URL链接是完整的,而不是当前形式的部分链接?我的代码的另一部分已经遍历了每个页面,当前将所有信息收集到一个CSV中,我想将作业链接添加到该文件中,直到我可以将其转换为JSON格式,以便在我正在使用的系统上接收API。到目前为止,我已经尝试使用printlinks.lstrip
-- opportunities --
0) <a href="index.gp?method=cappportal.showJob&amp;layoutid=2092&amp;inp1541=&amp;inp1375=154761&amp;opportunityid=154761">
                                        2D Capture Artist - 6 month contract
                                    </a>
1) <a href="index.gp?method=cappportal.showJob&amp;layoutid=2092&amp;inp1541=&amp;inp1375=154426&amp;opportunityid=154426">
                                        Accounting Supervisor
                                    </a>
2) <a href="index.gp?method=cappportal.showJob&amp;layoutid=2092&amp;inp1541=&amp;inp1375=152147&amp;opportunityid=152147">
                                        Advanced Analyst
                                    </a>
3) <a href="index.gp?method=cappportal.showJob&amp;layoutid=2092&amp;inp1541=&amp;inp1375=153395&amp;opportunityid=153395">
                                        Advanced UX Researcher
                                    </a>
4) <a href="index.gp?method=cappportal.showJob&amp;layoutid=2092&amp;inp1541=&amp;inp1375=151309&amp;opportunityid=151309">
                                        AI Engineer
                                    </a>
5) <a href="index.gp?method=cappportal.showJob&amp;layoutid=2092&amp;inp1541=&amp;inp1375=150468&amp;opportunityid=150468">
                                        AI Scientist
                                    </a>
6) <a href="index.gp?method=cappportal.showJob&amp;layoutid=2092&amp;inp1541=&amp;inp1375=151310&amp;opportunityid=151310">
                                        AI Scientist - NLP Focus
                                    </a>
7) <a href="index.gp?method=cappportal.showJob&amp;layoutid=2092&amp;inp1541=&amp;inp1375=153351&amp;opportunityid=153351">
                                        AI Software Engineer (Apex Legends)
                                    </a>
8) <a href="index.gp?method=cappportal.showJob&amp;layoutid=2092&amp;inp1541=&amp;inp1375=152737&amp;opportunityid=152737">
                                        AI Software Engineer (Frostbite)
                                    </a>
9) <a href="index.gp?method=cappportal.showJob&amp;layoutid=2092&amp;inp1541=&amp;inp1375=154764&amp;opportunityid=154764">
                                        Analyste Qualité Sénior / Senior Quality Analyst
                                    </a>
10) <a href="index.gp?method=cappportal.showJob&amp;layoutid=2092&amp;inp1541=&amp;inp1375=153948&amp;opportunityid=153948">
                                        Animator 1
                                    </a>
11) <a href="index.gp?method=cappportal.showJob&amp;layoutid=2092&amp;inp1541=&amp;inp1375=151353&amp;opportunityid=151353">
                                        Applications Agreement Analyst
                                    </a>
12) <a href="index.gp?method=cappportal.showJob&amp;layoutid=2092&amp;inp1541=&amp;inp1375=154668&amp;opportunityid=154668">
                                        AR Analyst I
                                    </a>
13) <a href="index.gp?method=cappportal.showJob&amp;layoutid=2092&amp;inp1541=&amp;inp1375=153609&amp;opportunityid=153609">
                                        AR Specialist
                                    </a>
14) <a href="index.gp?method=cappportal.showJob&amp;layoutid=2092&amp;inp1541=&amp;inp1375=154773&amp;opportunityid=154773">
                                        Artiste Audio / Audio Artist
                                    </a>