| 
 | 
 
 
发表于 2020-12-24 13:10:15
|
显示全部楼层
 
 
 
 
没有遇到编码问题呢 
 
- import requests
 
 - from lxml import etree
 
  
- url="https://tieba.baidu.com/f?ie=utf-8&kw=python"
 
 - urla="https://tieba.baidu.com"
 
 - res=requests.get(url)
 
 - tree = etree.HTML(res.text)
 
 - data = tree.xpath("//ul[@id='thread_list']/li")
 
 - result=[]
 
 - for x in range(2,len(data)+1):
 
 -      title= tree.xpath("//ul[@id='thread_list']/li[%d]/div/div[2]/div[1]/div[1]/a/text()"%x)
 
 -      href= tree.xpath("//ul[@id='thread_list']/li[%d]/div/div[2]/div[1]/div[1]/a/@href"%x)
 
 -      result.append({title[0]:urla+href[0]})
 
 - for x in result:
 
 -     print(x)     
 
  复制代码 
 
D:\wp>py app4.py 
{'天无绝人之路,终于通过艰苦的自学python拿到了第一份offer': 'https://tieba.baidu.com/p/7160913009'} 
{'新人来求救,想秃我了': 'https://tieba.baidu.com/p/7165183847'} 
{'以就业为目的自学Python    如何系统学习不走弯路!': 'https://tieba.baidu.com/p/7051818149'} 
{'谁能做58房东数据的其他平台也可,有偿': 'https://tieba.baidu.com/p/6645144721'} 
{'半路出家自学Python,几个月的努力,终于拿到了一份offer': 'https://tieba.baidu.com/p/7052045317'} 
{'点开第一个程序闪退': 'https://tieba.baidu.com/p/7163190746'} 
{'Pygame.': 'https://tieba.baidu.com/p/7165745438'} 
{'求助大佬。': 'https://tieba.baidu.com/p/7165745384'} 
{'公司最近给了我一个难题,要我爬取app上面的数据,这个咋整': 'https://tieba.baidu.com/p/7165564145'} 
{'有偿  python作业   求大神': 'https://tieba.baidu.com/p/7109235114'} 
{'请大佬fu dao作业': 'https://tieba.baidu.com/p/7165398607'} 
{'随缘解答问题~♥': 'https://tieba.baidu.com/p/7111231436'} 
{'【为你解决编程遇到的难题】': 'https://tieba.baidu.com/p/7165359201'} 
{'兄弟们求助 关于xlwt模块xlrd有个. sheet _b': 'https://tieba.baidu.com/p/7165578782'} 
{'两千人python自学交流峮     人多势众  大佬很多': 'https://tieba.baidu.com/p/7063431560'} 
{'怎么配置python环境啊 安装的时候自动配置不成功手动配置': 'https://tieba.baidu.com/p/7165599533'} 
{'接帮做python与解决问题': 'https://tieba.baidu.com/p/7147649069'} 
{'【打卡自学】跟着B站的小甲鱼视频学的': 'https://tieba.baidu.com/p/7165668557'} 
{'Python爬虫': 'https://tieba.baidu.com/p/7160666274'} 
{'求助': 'https://tieba.baidu.com/p/7165652134'} 
{'萌新求问,初学者下个哪个版本的python比较好,求推荐谢谢': 'https://tieba.baidu.com/p/7165532082'} 
{'有没有需要《python程序设计基础》这本书课后答案的?': 'https://tieba.baidu.com/p/6920886303'} 
{'有木有大佬帮忙指导python课设的呀,价格可议,主要是爬虫': 'https://tieba.baidu.com/p/7155538373'} 
{'一个很简单但是想不明白的程序': 'https://tieba.baidu.com/p/7163838237'} 
{'求python大作业,急急急!': 'https://tieba.baidu.com/p/7147526471'} 
{'有Python的问题的可以找我哈': 'https://tieba.baidu.com/p/7162429297'} 
{'入门django的话,有什么网站推荐的吗': 'https://tieba.baidu.com/p/7165117595'} 
{'有没有爬虫需求的啊!': 'https://tieba.baidu.com/p/7162281022'} 
{'新手需要交python大作业,300行代码,50个知识点,欢': 'https://tieba.baidu.com/p/6762664919'} 
{'刚学死在第一步': 'https://tieba.baidu.com/p/7162317780'} 
{'跪求一位大神。': 'https://tieba.baidu.com/p/7155201436'} 
{'取得n个数,从小到大排列怎么弄啊求大神': 'https://tieba.baidu.com/p/7163685662'} 
{'打算转行IT,新手该如何入门': 'https://tieba.baidu.com/p/7144294733'} 
{'请大神们看看这个错误要怎么改': 'https://tieba.baidu.com/p/7164158538'} 
{'小白帮我看看': 'https://tieba.baidu.com/p/7164317899'} 
{'使用pycharm连接mysql数据库,出现这个错误怎么解决': 'https://tieba.baidu.com/p/7164287755'} 
{'想问一下3_5题为啥是错的求解答': 'https://tieba.baidu.com/p/7163961987'} 
{'找朋友一起开始从小白学习Python': 'https://tieba.baidu.com/p/7164751095'} 
{'python大作业': 'https://tieba.baidu.com/p/5729619055'} 
{'爬贴吧时,html已经用utf8编码了,为什么打印的html': 'https://tieba.baidu.com/p/7165111245'} 
{'求解!求救啊': 'https://tieba.baidu.com/p/7164939902'} 
{'大神看看第四题!!!': 'https://tieba.baidu.com/p/7165024303'} 
{'求进Python群': 'https://tieba.baidu.com/p/7165103678'} 
{'python网络学习哪个平台好': 'https://tieba.baidu.com/p/7165315926'} 
{'刚学Python没多久,新手太难了,求大佬帮助': 'https://tieba.baidu.com/p/7165420237'} 
{'python selenium 调用chrome时,一直停留在data界面不能跳转': 'https://tieba.baidu.com/p/6588242377'} 
{'求助': 'https://tieba.baidu.com/p/7165208635'} 
{'在线摸鱼党帮做作业了': 'https://tieba.baidu.com/p/7097565305'} 
{'来个靠谱的大哥帮忙做几个作业,价钱好说': 'https://tieba.baidu.com/p/7150059059'} |   
 
 
 
 |