Jump to content

Manual talk:Pywikibot/2022

Add topic
From mediawiki.org


Please use one of the communication channels listed on Manual:Pywikibot/Communication rather than using this discussion board. There is very little traffic here, so it may take a while before you get a response.

Check if a page exists

[edit]

If I use

if not pywikibot.Page.exists('Page'):
    #Do something

I get the error

[...]

   title = self._link.canonical_title()

AttributeError: 'str' object has no attribute '_link' CRITICAL: Exiting due to uncaught exception <class 'AttributeError'>

What I have to do? --ElBe (talk) 20:31, 20 January 2022 (UTC)Reply

I think you should use it like this:
if not pywikibot.Page('Page').exists():
178.209.138.12 (talk) 21:03, 20 January 2022 (UTC)Reply
Thank you! --ElBe (talk) 05:58, 21 January 2022 (UTC)Reply
It works if I use
[...]
page = pywikibot.Page(site, 'Page')
if not pywikibot.Page(page).exists():
   #Do something
Thank you! --ElBe (talk) 06:09, 21 January 2022 (UTC)Reply
Hi ElBe, this is not a proper idea but it works by a side effect:
exists()
is a method of a Page object, more precisely of a BasePage object. You have to create the object first to use it:
page = pywikibot.Page(site, 'Page')
if not page.exists(): pass # or do something sensefull
Your example works because
pywikibot.Page(page)
clones the original page and creates a new Page object. This functionality to implemented to upcast objects e.g.
fp = pywikibot.FilePage(page)
which creates a FilePage object and provides further methods. But keep in mind that page must be in File: namespace here.
Refer: https://doc.wikimedia.org/pywikibot/master/api_ref/pywikibot.page.html?highlight=exists#pywikibot.page.BasePage.exists  @xqt 09:53, 21 January 2022 (UTC)Reply

Count sections of a page

[edit]

Hello. How I can count the number of sections on a page? --ElBe (talk) 18:37, 10 February 2022 (UTC)Reply

You could create a script using extract_sections method from textlib (https://phabricator.wikimedia.org/diffusion/PWBC/browse/master/pywikibot/textlib.py). Or perhaps use external libraries like mwparserfromhell/wikitextparser to do that. Dvorapa (talk) 22:49, 10 February 2022 (UTC)Reply
Ok, thanks. I will try it. --ElBe (talk) 05:23, 11 February 2022 (UTC)Reply

Examples of bots and tools that rely on the Pywikibot framework

[edit]
I'm looking for examples of active, helpful, and popular bots and tools that rely on the Pywikibot framework that we can highlight in an upcoming workshop on the topic as part of the "Small wiki toolkits" initiative. If you could point me to some examples, that would be super useful. Thank you! User:SSethi_(WMF) 03:52, 22 February 2022 (UTC)Reply
https://doc.wikimedia.org/pywikibot/master/scripts/index.html  @xqt 05:50, 22 February 2022 (UTC)Reply
See w:Category:Wikipedia bots using Pywikibot; there are a few dead bots there, but also a few active ones, including double redirect fixing bots. * Pppery * it has begun 04:22, 22 February 2022 (UTC)Reply
[edit]

I'm trying the Manual:Pywikibot/weblinkchecker.py to obtain broken links on the page "Aeroflot" on test.wikipedia.org. It does generate a "deadlinks-wikipedia-test.dat" file. I am looking for a ".txt" file, but that isn't generated by re-running the command a second time as per instructions given in the manual. Also, I've modified the settings in "user-config.py" to set "report_dead_links_on_talk" to true to report the dead links on the talk page. That also doesn't seem to be working. Any leads? User:SSethi_(WMF) 20:56, 19 March 2022 (UTC)Reply

After how long did you re-run the script and what was your command (did you set -day)? Usually with most parameters left in defaults you have to run the script and then re-run the script between one and two weeks later. Only then it will generate the second txt file and talk pages memos as expected. Dvorapa (talk) 20:10, 22 March 2022 (UTC)Reply